<?php
$courtInformatinStr = Yii::t('BWeSport', 'Court Informatin');
$propertyStr = Yii::t('BWeSport', 'Property');
$contentStr = Yii::t('BWeSport', 'Content');
$courtNameStr = Yii::t('BWeSport', 'Court Name');
$courtAddressStr = Yii::t('BWeSport', 'Court Address');
$courtImgStr = Yii::t('BWeSport', 'Court Img');
$courtDescriptionStr = Yii::t('BWeSport', 'Court Description');
$closeStr = Yii::t('BWeSport', 'Close');
$saveCourtStr = Yii::t('BWeSport', 'Save Court');
//first initialize court if not find it in database
$courtRoot = BWAddress::getRoot();
$chongqing = $courtRoot->getChild('china.chongqing');

if (!$chongqing instanceof BWAddress || $chongqing->getId() == NULL) {
    $courtRoot->addChild('china.chongqing.yuzhong');
    $courtRoot->addChild('china.chongqing.shapingba');
    $courtRoot->addChild('china.chongqing.yangjiaping');
    $courtRoot->addChild('china.chongqing.nanping');
    $courtRoot->addChild('china.chongqing.banan');
}
?>
<?php
$model = new BDynamicFormModel();

$courtInputMedia = NULL;
$bookableMessage = new BWMessage($bookableId);
$courtInputMedia = $bookableMessage->getInputMediaManager()->getInputMedia('court');
$courtMessage = NULL;
if (isset($courtId) && $courtId > 0) {
    $courtMessage = new BWMessage($courtId);
    $myCourtId = $courtId;
}
/* else {
  $courtMessage = new BWMessage();
  $courtMessage->name = uniqid('court');
  $courtMessage->save();
  //re-calculate name
  $courtName = 'court' . $courtMessage->getId();
  $courtMessage->name = $courtName;
  $courtMessage->save();
  $courtInputMedia->getMessageManager()->addMessage($courtMessage);
  $myCourtId = $courtMessage->getId();
  }
 * 
 */
?>
<?php
$url = $control->getAjaxLinkUrl(
        'addBookableCourt', NULL, array(
    'courtId' => $courtId,
    'bookableId' => $bookableId,
    'eventId' => $eventId
        )
);
$updateId = $control->getAjaxContainerId();
?>
<script type="text/javascript">
// this script for collecting the form data and pass to the controller action and doing the on success validations
    function saveBookableCourt() {

        var formData = new FormData($("#message-form")[0]);
        //var formElement = document.getElementById("message-form");
        //var formData = new FormData(formElement);

        $.ajax({
            url: '<?php echo $url; ?>',
            type: 'POST',
            data: formData,
            datatype: 'json',
            // async: false,
            beforeSend: function() {
                $("#ajax-loaderdiv").show();
            },
            success: function(data) {
                // on success do some validation or refresh the content div to display the uploaded images 
                jQuery("#<?php echo $updateId; ?>").html(data);
                //alert(data);
            },
            complete: function() {
                $("#ajax-loaderdiv").hide();
                $("#myModal").modal("hide");
            },
            error: function() {
                alert("There may a error on uploading. Try again later");
            },
            cache: false,
            contentType: false,
            processData: false
        });

        return false;
    }
</script>

<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    <h3><?php echo $courtInformatinStr; ?></h3>
</div>
<div class="modal-body" style="max-height: 300px;" >

    <table class="table table-hover">
        <colgroup>
            <col class="span3">
            <col class="span6">
        </colgroup>
        <thead>
            <tr>
                <th><?php echo $propertyStr; ?></th>
                <th><?php echo $contentStr; ?></th>
            </tr>
        </thead>
        <tbody>

            <?php
            //$courtName = $courtMessage->getName();
            //add media for inputable value
            $displayNameOfCourt = 'media_displayName_message_court';
            $addressOfCourt = 'media_address_message_court';
            $imageOfCourt = 'image_media_image_message_court';
            $descriptionOfCourt = 'media_description_message_court';
            $longitudeOfCourt = 'media_longitude_message_court';
            $latitudeOfCourt = 'media_latitude_message_court';
            $model->addField($longitudeOfCourt);
            $model->addField($latitudeOfCourt);

            $model->addField($displayNameOfCourt);
            $model->addField($addressOfCourt);
            $model->addField($imageOfCourt);
            $model->addField($descriptionOfCourt);

            if ($courtId > 0 && $courtMessage instanceof BWMessage) {
                $displayNameMedia = $courtMessage->getMediaManager()->getMedia('displayName');
                if ($displayNameMedia instanceof BWMedia)
                    $model->$displayNameOfCourt = $displayNameMedia->getContent();
                $addressMedia = $courtMessage->getMediaManager()->getMedia('address');
                if ($addressMedia instanceof BWMedia)
                    $model->$addressOfCourt = $addressMedia->getContent();
                $imageMedia = $courtMessage->getMediaManager()->getMedia('image');
                if ($imageMedia instanceof BWMedia)
                    $model->$imageOfCourt = $imageMedia->getContent();
                $descriptionMedia = $courtMessage->getMediaManager()->getMedia('description');
                if ($descriptionMedia instanceof BWMedia)
                    $model->$descriptionOfCourt = $descriptionMedia->getContent();
                 //below code for get map data from table
                $longitudeMedia = $courtMessage->getMediaManager()->getMedia('longitude');
                if ($longitudeMedia instanceof BWMedia)
                    $model->$longitudeOfCourt = $longitudeMedia->getContent();
                $latitudeMedia = $courtMessage->getMediaManager()->getMedia('latitude');
                if ($latitudeMedia instanceof BWMedia)
                    $model->$latitudeOfCourt = $latitudeMedia->getContent();
            }
            /*
              if ($courtId > 0) {
              $mapTable = new BSheet('mapTable');
              //  $list = $mapTable->getList($courtId);
              $list = $mapTable->getList(array('itemData'=>array('longitude'=>'longitude')));
              $model->$longitude = $list->getItem('longitude')->getValue();
              $model->$latitude =  $list->getItem('latitude')->getValue();

              //             $list只会有一条数据，所有这样取出。
              //             foreach ($list as $id->$_v)
              //             $longitude = $_v->getItem('longitude')->getValue();
              //             $latitude = $_v->getItem('latitude')->getValue();
              }
             * 
             */
            ?>
            <tr>
                <td>
                    <?php echo '<nobr style="color: red;font-size:20px">*</nobr>', $courtNameStr; ?>
                </td>
                <td>
                    <?php
                    echo CHtml::activeTextField($model, $displayNameOfCourt, array('placeholder' => $courtNameStr . '...', 'required' => true));
                    ?>
                </td>
            </tr>
            <tr>
                <td>
                    <?php echo '<nobr style="color: red;font-size:20px">*</nobr>', $courtAddressStr; ?>
                </td>
                <td>
                    <?php
                    echo CHtml::activeTextField($model, $addressOfCourt, array('placeholder' => $courtAddressStr . '...','id'=>'address', 'required' => true));
                    ?>
                </td>
            </tr>
            <tr><td> <?php echo '<nobr style="color: red;font-size:20px">*</nobr>', $courtImgStr; ?> </td>
                <td>
                    <?php
                    echo CHtml::activeFileField($model, $imageOfCourt);
                    if ($model->$imageOfCourt != NULL) {
                        $imageUrl = Yii::app()->request->baseUrl . $model->$imageOfCourt;
                        echo "<img src=" . $imageUrl . " width=50 height=50 />";
                    }
                    ?>                    
                </td>
            </tr>               
            <tr>
                <td><?php echo $courtDescriptionStr; ?>  </td>                    
                <td>
                    <?php
                    echo CHtml::activeTextField($model, $descriptionOfCourt, array('placeholder' => '...'));
                    ?>
                </td>
            </tr>

            <tr>
                <td>map标点</td>
                <td>
                    <style type="text/css">
                        #allmap {width:400px;height: 260px;overflow: hidden;margin:0;font-family:"微软雅黑";}
                    </style>
                    <div id="allmap"></div>
    经度:<!--<input id="jing" name="jing" type="text" value=""/>-->
                    <?php
                    echo CHtml::activeTextField($model, $longitudeOfCourt, array('placeholder' => '请点击地图生成', 'id' => 'jing'));
                    ?>
    <br/>纬度:<!--<input id="wei" type="text" value=""/>-->
                    <?php
                    echo CHtml::activeTextField($model, $latitudeOfCourt, array('placeholder' => '请点击地图生成', 'id' => 'wei'));
                    ?>
                    <script type="text/javascript">
                        //百度地图API功能
                        (function() {
                            window.BMap_loadScriptTime = (new Date).getTime();
                            window.BMap = window.BMap || {};
                            window.BMap.apiLoad = function() {
                                delete window.BMap.apiLoad;
                                if (typeof init == "function") {
                                    init()
                                }
                            };
                            var s = document.createElement('script');
                            s.src = 'http://api.map.baidu.com/getscript?v=2.0&ak=258b70c4a5648db1e0a306d017a3c8f6&services=&t=20140930184510';
                            document.body.appendChild(s);
                        })();
                        function init() {
                            var map = new BMap.Map("allmap");
                            map.centerAndZoom("重庆", 12);
                            map.enableScrollWheelZoom();                 //启用滚轮放大缩小
                            var address = document.getElementById("address").value;
                            var x = document.getElementById("jing").value;
                            var y = document.getElementById("wei").value
                            var point = new BMap.Point(x, y);
                            var marker = new BMap.Marker(point);  
                            map.addOverlay(marker);               
                            marker.setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的动画
                            var label = new BMap.Label(address,{offset:new BMap.Size(20,-10)});
                            marker.setLabel(label);
        
                            //单击获取点击的经纬度
                            map.addEventListener("click", function(e) {
                                var lng = e.point.lng;
                                var lat = e.point.lat;
                                var address = document.getElementById("address").value;
                                document.getElementById("jing").value = lng;
                                document.getElementById("wei").value = lat;
                                
                                var point = new BMap.Point(lng, lat);
                                var marker = new BMap.Marker(point);  
                                map.addOverlay(marker);               
                                marker.setAnimation(BMAP_ANIMATION_BOUNCE);
                                var label = new BMap.Label(address,{offset:new BMap.Size(15,-30)});
                                marker.setLabel(label);

                                var allOverlay = map.getOverlays();
                                for (var i = 0; i < allOverlay.length -1; i++){
                                        map.removeOverlay(allOverlay[i]);
                                }   
                            });

                            var top_left_control = new BMap.ScaleControl({anchor: BMAP_ANCHOR_TOP_LEFT});// 左上角，添加比例尺
                            var top_left_navigation = new BMap.NavigationControl();  //左上角，添加默认缩放平移控件
                            var top_right_navigation = new BMap.NavigationControl({anchor: BMAP_ANCHOR_TOP_RIGHT, type: BMAP_NAVIGATION_CONTROL_SMALL}); //右上角，仅包含平移和缩放按钮
                            map.addControl(top_left_control);
                            map.addControl(top_left_navigation);
                            map.addControl(top_right_navigation);
                            
                            
                        }
                    </script>
                </td>
            </tr>
        </tbody>
    </table>
</div>

<div class="modal-footer">
    <button class="btn" data-dismiss="modal" aria-hidden="true"><?php echo $closeStr; ?></button>
    <button class="btn" data-dismiss="modal" aria-hidden="true" onclick="javascript:saveBookableCourt()"><?php echo $saveCourtStr; ?></button>
</div>


